/**
 * RecLSASlot.java
 *
 * @author Hagen Boehm
 */


package SSF.OS.OSPFv2;


import SSF.OS.ProtocolException;

// ============================= class SSF.OS.OSPFv2.RecLSASlot ================================= //
/**
 *  This class extends LSASlot. It is intended only for storing LSAs received from a Neighbor! 
 */
public class RecLSASlot extends LSASlot {

  // boolean value which indicates write-protection of the stored LSA.
  private boolean isProtected;
  // After expiration of this timer a new instance of the stored LSA can be installed.
  public MinimumArrivalTimer minLSArrivalTimer;
  // Duration of MinimumArrivalTimer.
  public long minLSArrival;

  // ------------------------------- constructor RecLSASlot ----------------------------------- //
  public RecLSASlot(LSA lsa, OSPF myOSPF) {
    super(lsa, myOSPF);
    this.minLSArrival = myOSPF.MIN_LS_ARRIVAL;
    isProtected = true;
    minLSArrivalTimer = new MinimumArrivalTimer(myOSPF, minLSArrival);
    minLSArrivalTimer.set(minLSArrival);
  }

  // -------------------------------- RecLSASlot.install -------------------------------------- //
  /** installs a new instance of an existing LSA in this slot. */
  public void install(LSA adv) throws ProtocolException {
    if (isProtected) throw new ProtocolException("Could not install new instance of LSA \"" +
      adv.getKey() + "\"! (MinLSArrival)");
    super.install(adv);
    isProtected = true;
    minLSArrivalTimer = new MinimumArrivalTimer(myOSPF, minLSArrival);
    minLSArrivalTimer.set(minLSArrival);
  }

  // =========================== inner class MinimumArrivalTimer ============================== //
  /**
  /* The MinimumArrivalTimer waits minLSArrival seconds before it cancels the write-protection
   * for the LSA stored in the Slot. A new instance of the LSA can be installed after expiration
   * of the Timer only.
   */
  
  public class MinimumArrivalTimer extends SSF.OS.Timer {
    
    // ------------------------- constructor MinimumArrivalTimer ------------------------------ //

    public MinimumArrivalTimer(OSPF o, long dt) {
      super(o.inGraph(), dt);
    }

    // -------------------------- MinimumArrivalTimer.callback -------------------------------- //
    public void callback() {
      isProtected = false;
    }

  } // end of class MinimumArrivalTimer

}
